//
//  Status.swift
//  SinaWeibo25
//
//  Created by apple on 16/3/15.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import AFNetworking
class Status: NSObject {
    
    var created_at: String?
    
    var id: Int64 = 0
    
    var text: String?
    
    var source: String?
    
    var retweeted_status: Status?
    
    var user: User?
    //配图字典数组
    var pic_urls: [[String : String]]? {
        didSet {
            guard let urls = pic_urls else {
                //如果pic_urls为空 直接不给 imageURLs赋值
                return
            }
            imageURls = [NSURL]()

            for item in urls {
                let urlString = item["thumbnail_pic"]
                //相信后端给定的 第一个 '!' urlString  一定存在
                //第二个 '!' 相信后端给定的url一定可以得到NSURL对象
                let url = NSURL(string: urlString!)!
                imageURls?.append(url)
            }
        }
    }
//    配图
    var imageURls: [NSURL]?
    
    init(dict: [String : AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    override func setValue(value: AnyObject?, forKey key: String) {
        if key == "user" {
            guard let dict = value as? [String : AnyObject] else {
                //不能转换为字典类型
                return
            }
            user = User(dict: dict)
            return
        }
        
        if key == "retweeted_status" {
            guard let dict = value as? [String : AnyObject] else {
                //不能转换为字典类型
                return
            }
            
            //字典转模型
            retweeted_status = Status(dict: dict)
            //必须要添加return  不然字典转模型就白做了
            return
        }
        
        super.setValue(value, forKey: key)
    }
    
//    过滤
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {

    }
    
    
//    重写 描述 方法
    override var description: String {
        let keys = ["id","text","created_at","source"]
        let dict = self.dictionaryWithValuesForKeys(keys)
        return dict.description
    }
}


extension Status {
    class func loadHome(finished:(array: [Status]?)->()) {
        guard let token = UserAccount.shareAccount.access_token else {
            print("用户为登录,或者登录过期")
            return
        }
        let urlstring = "https://api.weibo.com/2/statuses/home_timeline.json"
        let parameters = ["access_token" : token]
        let AFN = AFHTTPSessionManager()
        
        AFN.GET(urlstring, parameters: parameters, progress: nil, success: { (_, result) -> Void in
            print(result)
            guard let dict = result as? [String : AnyObject] else {
                finished(array: nil)
                print("数据结构错误")
                return
            }
            guard let array = dict["statuses"] as? [[String : AnyObject]] else {
                finished(array: nil)
                print("数据结构错误")
                return
            }
            //实例化临时数组
            var temparry = [Status]()
//            遍历数组
            for item in array {
//                字典转模型
                let s = Status(dict: item)
//               添加模型数组
                temparry.append(s)
            }
//            刷新页面
            finished(array: temparry)
           
            }) { (_, errno_t) -> Void in
                print(errno_t)
                
        }
        
        
    }
}